﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_OtherListTable_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            getLoai()
            getNgonngu()

        End If
    End Sub
    Protected Sub getNgonngu()
        ddlLanguageId.Items.Clear()
        ddlLanguageId.AppendDataBoundItems = True
        ddlLanguageId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.Languages Select q.LanguageId, q.Name).ToList
            ddlLanguageId.DataValueField = "LanguageId"
            ddlLanguageId.DataTextField = "Name"
            ddlLanguageId.DataSource = p
            ddlLanguageId.DataBind()
            ddlLanguageId.SelectedValue = 0
        End Using
    End Sub
    Protected Sub getLoai()
        ddlTypeId.Items.Clear()
        ddlTypeId.AppendDataBoundItems = True
        ddlTypeId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.OtherListTableTypes Select q.Id, q.Name).ToList
            ddlTypeId.DataValueField = "Id"
            ddlTypeId.DataTextField = "Name"
            ddlTypeId.DataSource = p
            ddlTypeId.DataBind()
            ddlTypeId.SelectedValue = 0
        End Using
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.Otherlisttable
            Try
                p.OLCode = txtOLCode.Text.Trim()
                p.OtherListName = txtOtherListName.Text.Trim()
                p.Note = txtNote.Text.Trim()
                p.TypeId = ddlTypeId.SelectedValue()
                p.LanguageId = ddlLanguageId.SelectedValue()
                data.Otherlisttables.AddObject(p)
                data.SaveChanges()
                Insert_App_Log("Insert  Otherlisttable:" & txtOLCode.Text.Trim & "", Function_Name.OtherListTable, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub ResetControl()
        txtOLCode.Text = ""
        txtOtherListName.Text = ""

        txtNote.Text = ""
        ddlTypeId.SelectedIndex = -1
        ddlLanguageId.SelectedIndex = -1
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/OtherListTable/List.aspx';", Me.Page, True)
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
